package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.LeaseTermService;
import com.atguigu.lease.web.app.service.PaymentTypeService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    RoomInfoMapper roomInfoMapper;

    @Autowired
    PaymentTypeMapper paymentTypeMapper;

    @Autowired
    LeaseTermMapper leaseTermMapper;

    @Autowired
    GraphInfoMapper graphInfoMapper;

    @Override
    public  List<AgreementItemVo> listAgreeVoByphone(String phone) {
        return leaseAgreementMapper.listAgreeVoByphone(phone);
    }

    @Override
    public AgreementDetailVo getAgreeDetailVo(Long id) {
        //根据租约id查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);


        //根据租约信息中的公寓id查询公寓名称
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());
        agreementDetailVo.setApartmentName(apartmentInfo.getName());

        //根据租约信息中的房间id查询 房间号
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());

        //支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
        agreementDetailVo.setPaymentTypeName(paymentType.getName());

        //租期月数
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
        agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());

        //租期单位
        agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());

        //公寓图片列表
        LambdaQueryWrapper<GraphInfo> graphVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemId,leaseAgreement.getApartmentId());
        List<GraphInfo> graphInfos = graphInfoMapper.selectList(graphVoLambdaQueryWrapper);

        List<GraphVo> listGra = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo = new GraphVo();
            graphVo.setUrl(graphInfo.getUrl());
            graphVo.setName(graphInfo.getName());
            listGra.add(graphVo);
        }

        agreementDetailVo.setApartmentGraphVoList(listGra);

        //房间图片列表
        LambdaQueryWrapper<GraphInfo> graphVoLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphVoLambdaQueryWrapper.eq(GraphInfo::getItemId,leaseAgreement.getRoomId());
        List<GraphInfo> graphInfos2 = graphInfoMapper.selectList(graphVoLambdaQueryWrapper);


        List<GraphVo> listGra2 = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos2) {
            GraphVo graphVo = new GraphVo();
            graphVo.setUrl(graphInfo.getUrl());
            graphVo.setName(graphInfo.getName());
            listGra2.add(graphVo);
        }
        agreementDetailVo.setRoomGraphVoList(listGra2);

        return agreementDetailVo;
    }
}




